<template>
    <div>
        <div v-show="false">{{initImgsList}}</div>
        <div class="my-upload-list" v-for="item in uploadList">
            <template v-if="item.status === 'finished'">
                <img :src="item.url">
                <div class="my-upload-list-cover">
                    <Icon type="ios-eye-outline" @click.native="handleView(item.url)"></Icon>
                    <Icon v-show="canOp" type="ios-trash-outline" @click.native="delImg(item)"></Icon>
                </div>
            </template>
            <template v-else>
                <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
            </template>
        </div>
        <Upload v-show="addMore && canOp"
                ref="upload"
                :show-upload-list="false"
                :default-file-list="defaultList"
                :on-success="handleSuccess"
                :format="['jpg','jpeg','png']"
                :max-size="2048"
                :on-format-error="handleFormatError"
                :on-exceeded-size="handleMaxSize"
                :before-upload="handleBeforeUpload"
                :data="uploadParams"
                :action="action_url"
                multiple
                type="drag"
                style="display: inline-block;width:58px;">
            <div style="width: 58px;height:58px;line-height: 58px;" v-show="addMore">
                <Icon type="ios-camera" size="20"></Icon>
            </div>
        </Upload>
        <Modal title="View Image" v-model="visible">
            <img :src="imgName + '!pre'" v-if="visible" style="width: 100%">
        </Modal>
    </div>

</template>
<script>
    import { del } from '@/api/oss'
    import util from '../libs/util.js';
    const QN = 0;
    const OSS = 1;
    export default {
        name: 'ossupload',
        data () {
            return {
                addMore: true,
                imgName: '',
                visible: false,
                uploadParams: {},
                uploadList: [],
                items: [],
                action_url: 'api/admin/Oss/uploadFile',
                baseUrl: '',
                type: QN,
                key: '',
                defaultList: []
            };
        },
        props: {
            initList:  {
              type: Array,
              default: function () {
                return []
              }
            },
            countLimit: {type: Number, default: 0},
            canOp: {type: Boolean, default: true}, // 标记是否可以操作删除 添加更多
            pre: {type: String, default: 'qukan/'},
            delimg: Function
        },
        created () {
        },
        methods: {
            handleView (url) {
                this.imgName = url;
                this.visible = true;
            },
            handleRemove (file) {
                const fileList = this.$refs.upload.fileList;
                this.uploadList.splice(fileList.indexOf(file), 1);
                this.checkLimit();
            },
            handleSuccess (res, file) {
                file.url = res.data.url;
                file.name = res.data.name;
                this.$emit('imgs', this.uploadList);
                this.checkLimit();
            },
            handleFormatError (file) {
                this.$Notice.warning({
                    title: '格式错误',
                    desc: file.name + ' 格式错误, 请选择 jpg 或者 png.'
                });
            },
            handleMaxSize (file) {
                this.$Notice.warning({
                    title: '超过文件大小限制',
                    desc: '文件最大不超过 2M.'
                });
            },
            handleBeforeUpload (file) {
                // 如果是0则不限制
                if (this.countLimit > 0) {
                    const check = this.uploadList.length < this.countLimit;
                    if (!check) {
                        this.$Notice.warning({
                            title: '最多上传' + this.countLimit + '张图片'
                        });
                    }
                    return check;
                }
                return true;
            },
            checkLimit () {
                if (this.countLimit > 0) {
                    if (this.uploadList.length === this.countLimit) {
                        this.addMore = false;
                    } else {
                        this.addMore = true;
                    }
                }
            },
            delImg (file) {
                let vm = this;
                del(file.name).then(response => {
                      let res = response.data;
                      vm.handleRemove(file);
                      vm.$Message.success('删除成功');
                      if (vm.delimg) {
                          vm.delimg(vm.uploadList);
                      }
                  });
            },
            updateImgsList () {
                if (this.$refs.upload !== undefined && this.$refs.upload.fileList !== undefined) {
                    this.uploadList = this.$refs.upload.fileList;
                }
            }
        },
        computed: {
            // 通过计算属性来监测数据变化
            initImgsList () {
                let vm = this;
                vm.updateImgsList();
                return vm.uploadList;
            }
        },
        watch: {
            initList: {
                immediate: true,
                handler (fileList) {
                    let vm = this;
                    vm.defaultList.splice(0, vm.defaultList.length);
                    if (vm.initList !== null && vm.initList.length > 0) {
                        vm.initList.forEach(v => {
                            vm.defaultList.push({name: v, url: v});
                        });
                    }

                }
            }
        },
        mounted () {
            this.uploadList = this.$refs.upload.fileList;
        }
    };
</script>
<style scoped lang="less">
    .my-upload-list {
        display: inline-block;
        width: 60px;
        height: 60px;
        text-align: center;
        line-height: 60px;
        border: 1px solid transparent;
        border-radius: 4px;
        overflow: hidden;
        background: #fff;
        position: relative;
        box-shadow: 0 1px 1px rgba(0, 0, 0, .2);
        margin-right: 4px;
    }

    .my-upload-list img {
        width: 100%;
        height: 100%;
    }

    .my-upload-list-cover {
        display: none;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(0, 0, 0, .6);
    }

    .my-upload-list:hover .my-upload-list-cover {
        display: block;
    }

    .my-upload-list-cover i {
        color: #fff;
        font-size: 20px;
        cursor: pointer;
        margin: 0 2px;
    }
</style>
